package com.jn.erp.web.api.sys.controller;

import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.sys.entity.SysNotice;
import com.jn.erp.web.api.sys.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 公告 信息操作处理
 *
 * @author jn
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseWebAuthController {
    @Autowired
    private SysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice) {
        notice.setOrgId(Long.valueOf(loginDeptAuth()));
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }


    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(getUserId());
        notice.setCreateTime(new Date());
        notice.setOrgId(Long.valueOf(loginDeptAuth()));
        return toAjax(noticeService.save(notice));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return success(noticeService.getById(noticeId));
    }


    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(getUserId());
        notice.setUpdateTime(new Date());
        return toAjax(noticeService.updateById(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        if(noticeIds.length>0){
            return error("至少选中一条数据");
        }
        return toAjax(noticeService.removeBatchByIds(Arrays.asList(noticeIds)));
    }


    /**
     * 发送通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:send')")
    @Log(title = "发送通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/sendBatchMessage")
    @Transactional
    public AjaxResult sendBatchMessage(@RequestBody Long[] noticeIds) {
        if(noticeIds.length>0){
            return error("至少选中一条数据");
        }
        //TODO 发送操作，公告发送全体用户，通知发送指定用户
        return success();
    }

}
